diff --git a/class-RegisterJS.php b/class-RegisterJS.php index 891fbe6..bd3b0f6 100644 --- a/class-RegisterJS.php +++ b/class-RegisterJS.php @@ -1,165 +1,178 @@ . /** * Register and enqueue JS libraries */ class RegisterJS { /** * Default position */ public static $DEFAULT = 'header'; /** * Script names */ private $js = []; /** * Get the singleton instance * * @return self */ public static function instance() { static $me = false; if( ! $me ) { $me = new self(); } return $me; } /** * Register a new script name * - * @param string $name JS name, like: "jquery" - * @param mixed $url Script url, like "http://example.org/lib/jquery.js" - * @param string $position header|footer + * @param string $name Script name, like: "jquery" + * @param mixed $url Script url, like "http://example.org/lib/jquery.js" + * @param string $position header|footer + * @param array $dependencies Dependent script names */ - public function register( $uid, $url, $position = null ) { + public function register( $uid, $url, $position = null, $dependencies = [] ) { if( ! $position ) { $position = self::$DEFAULT; } - $this->js[ $uid ] = new JS( $uid, $url, $position ); + $this->js[ $uid ] = new JS( $uid, $url, $position, $dependencies ); } /** * Register an inline script * * @param $name string * @param $data string * @param $position before|after */ public function registerInline( $uid, $data, $position ) { $this->js[ $uid ]->inline[ $position ][] = $data; } /** * Enqueue a previous registered JS name * * @param $name string JS name * @param $position string Place it in the head of the page or not */ public function enqueue( $uid, $position = null ) { $js = $this->js[ $uid ]; - $js->enqueue = true; + + // eventually override script position if( $position ) { $js->position = $position; } + + // eventually mark to be enqueued with also its dependencies + if( !$js->enqueue ) { + $js->enqueue = true; + foreach( $js->dependencies as $dependency_uid ) { + $this->enqueue( $dependency_uid ); + } + } } /** * Print all the JS from a specified position * * @param $position string */ public function printAll( $position ) { $glue = $position === 'header' ? "\n\t" : "\n"; foreach( $this->js as $js ) { if( $js->enqueue && $js->position === $position ) { $js->printInline( 'before', $glue ); $js->printNormal( $glue ); $js->printInline( 'after', $glue ); } } } } class JS { public $uid; public $url; public $position; public $enqueue = false; public $inline; + public $dependencies; + /** * Construct * * @param $uid string * @param $url string * @param $position string header|footer */ - public function __construct( $uid, $url, $position ) { + public function __construct( $uid, $url, $position, $dependencies ) { $this->uid = $uid; $this->url = $url; $this->position = $position; + $this->dependencies = $dependencies; $this->inline = [ 'after' => [], 'before' => [], ]; } /** * Print inline JavaScript parts * * @param $position before|after * @param $glue string */ public function printInline( $position, $glue ) { $parts = $this->inline[ $position ]; if( $parts ) { echo "$glue"; if( DEBUG ) { echo ""; } } } /** * Print the normal JS inclusion * * @param $glue string */ public function printNormal( $glue ) { $url = $this->url; if( CACHE_BUSTER ) { $url .= false === strpos( $url, '?' ) ? '?' : '&'; $url .= CACHE_BUSTER; } echo "$glue"; if( DEBUG ) { echo ""; } } } diff --git a/functions.php b/functions.php index c30b3b8..4cbdd43 100644 --- a/functions.php +++ b/functions.php @@ -1,1119 +1,1120 @@ . /** * Check if the user do not want to be tracked * * See the HTTP 'DNT' header. * * @return boolean */ function do_not_track() { return isset( $_SERVER['HTTP_DNT'] ) && $_SERVER['HTTP_DNT'] === '1'; } /* * Configuration helpers * * Shortcuts useful when you declare a configuration file. */ /** * Define a constant if it does not exist * * @param string $name Constant name * @param string $value Constant value */ function define_default( $name, $value ) { defined( $name ) or define( $name, $value ); } /* * Template helpers * * Shortcuts useful when you build a page */ /** * Return a ' selected="selected"' HTML attribute under some circumstances * * @param mixed $helper If this is the only arg, return * @param mixed $current If this matches $helper, return * @param bool $force If this is true, return */ function selected( $helper = PHP_INT_MAX, $current = PHP_INT_MAX, $force = false ) { return html_attribute_when_matching( 'selected', 'selected', $helper, $current, $force); } /** * Return a ' checked="checked"' HTML attribute under some circumstances * * This is useful for the HTML tag. * * @param mixed $helper If this is the only arg, return * @param mixed $current If this matches $helper, return * @param bool $force If this is true, return */ function checked( $helper = PHP_INT_MAX, $current = PHP_INT_MAX, $force = false ) { return html_attribute_when_matching( 'checked', 'checked', $helper, $current, $force); } /** * Return a ' disabled="disabled"' HTML attribute under some circumstances * * @param mixed $helper If this is the only arg, return * @param mixed $current If this matches $helper, return * @param bool $force If this is true, return */ function disabled( $helper = PHP_INT_MAX, $current = PHP_INT_MAX, $force = false ) { return html_attribute_when_matching( 'disabled', 'disabled', $helper, $current, $force ); } /** * Return an HTML attribute under some circumstances * * @param string $attribute HTML attribute name e.g. 'disabled' * @param string $value HTML attribute value e.g. * @param mixed $helper If this is the only arg, return * @param mixed $current If this matches $helper, return * @param bool $force If this is true, return */ function html_attribute_when_matching( $attribute, $value, $helper = PHP_INT_MAX, $current = PHP_INT_MAX, $force = false ) { if( $helper === $current || $helper && PHP_INT_MAX === $current || $force ) { return HTML::property( $attribute, $value ); } return ''; } /** * Get an HTML attribute ' value="$v"' * * The value will be sanitized. * * @param string $v * @return string */ function value( $v ) { return HTML::property( 'value', $v ); } /** * Shortcut for htmlentities() * * Return an HTML-sanitized untrusted string to be safe from XSS. * * @param string $s e.g. 'Hello